package com.china_goldcard.horizontallistview;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by jk on 16/10/20.
 */
public class HorizontalMenuViewAdapter<T extends  HorizontalMenuViewModel,H extends HorizontalMenuViewHolder> extends RecyclerView.Adapter<H> {

    public interface OnItemClickListener{
        void onItemClick(View view,HorizontalMenuViewModel item,int position);
        void onItemLongClick(View view,HorizontalMenuViewModel item,int position);
    }

    private HorizontalMenuAdapter<H,T> dataAdapter;

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public HorizontalMenuAdapter<H, T> getDataAdapter() {
        return dataAdapter;
    }

    public void setDataAdapter(HorizontalMenuAdapter<H, T> dataAdapter) {
        this.dataAdapter = dataAdapter;
    }

    private List<T> datalist;

    public HorizontalMenuViewAdapter(List<T> datalist){
        this.datalist = datalist;
    }

    public void reloadData(List<T> datalist){
        this.datalist = datalist;
        notifyDataSetChanged();
    }

    @Override
    public H onCreateViewHolder(ViewGroup parent, int viewType) {
        if (dataAdapter != null){
           return dataAdapter.onCreateViewHolder(parent,viewType);
        }else{
           return null;
        }
    }

    @Override
    public void onBindViewHolder(H holder, final int position) {
        final T model = datalist.get(position);
        if (dataAdapter !=null){
            dataAdapter.onBindViewHolder(holder,position,model);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickLitener!=null){
                        mOnItemClickLitener.onItemClick(v,model,position);
                    }
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (mOnItemClickLitener!=null){
                        mOnItemClickLitener.onItemLongClick(v,model,position);
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }
}
